package by.gravity.pvh.utils;

import java.util.List;

import org.holoeverywhere.app.AlertDialog;
import org.holoeverywhere.widget.ArrayAdapter;
import org.holoeverywhere.widget.Toast;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.text.Editable;
import android.text.Selection;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ListView;
import by.gravity.pvh.R;
import by.gravity.pvh.data.SQLDataManager;

public class DialogUtil {

	public interface onEditCompleteListener {

		public void onEditCompelted(String value);
	}

	public interface onSelectCompleteListener {
		public void onSelected(String value);
	}

	public static void showConfirmDialog(Context context, int title, String message) {

		AlertDialog.Builder adb = new AlertDialog.Builder(context);

		adb.setTitle(title);

		adb.setMessage(message);

		adb.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {

				dialog.dismiss();
			}
		});

		adb.show();
	}

	public interface onPositiveListener {
		public void onPositionveClicked();
	}

	public static void showConfirmDialog(Context context, int title, String message, final onPositiveListener listener) {

		AlertDialog.Builder adb = new AlertDialog.Builder(context);

		adb.setTitle(title);

		adb.setMessage(message);

		adb.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				listener.onPositionveClicked();
				dialog.dismiss();
			}
		});

		adb.show();
	}

	public static void showEditDialog(final Activity activity, String title, int message, String value,
			final onEditCompleteListener onEditCompleteListener) {

		final EditText editText = new EditText(activity);
		editText.setText(value);

		int position = editText.length();
		Editable editable = editText.getText();
		Selection.setSelection(editable, position);

		AlertDialog.Builder alertDiaogBuilder = new AlertDialog.Builder(activity);

		alertDiaogBuilder.setTitle(title);
		if (message != 0) {
			alertDiaogBuilder.setMessage(message);
		}

		alertDiaogBuilder.setView(editText);

		alertDiaogBuilder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {

				String value = editText.getText().toString();

				onEditCompleteListener.onEditCompelted(value);
				dialog.dismiss();
			}
		});

		alertDiaogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {

				dialog.dismiss();
			}
		});
		alertDiaogBuilder.show();
	}

	public static void showListDialog(final Activity activity, int title, final List<String> list,
			final onSelectCompleteListener onSelectCompleteListener) {
		final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(activity, org.holoeverywhere.R.layout.select_dialog_item_holo);
		arrayAdapter.addAll(list);
		ListView listView = new ListView(activity);
		listView.setAdapter(arrayAdapter);

		builder.setView(listView);
		builder.setTitle(title);
		builder.setNegativeButton(R.string.cancel, new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();

			}
		});

		final AlertDialog dialog = builder.create();

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				onSelectCompleteListener.onSelected(list.get(position));
				dialog.dismiss();
			}
		});

		listView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
				showConfirmDialog(activity, R.string.delete,
						String.format(activity.getString(R.string.deleteWithName), arrayAdapter.getItem(position)),
						new onPositiveListener() {

							@Override
							public void onPositionveClicked() {
								String name = arrayAdapter.getItem(position);
								SQLDataManager.getInstance(activity).deleteOrder(name);
								arrayAdapter.remove(name);
							}
						});
				return false;
			}
		});

		dialog.show();
	}
}
